package com.mmche.filter;
/***********************************************************************
*
* @model   公共Web检测过滤器
* @author  wjk
* @version 1.0
* @purpose 业务系统利用此类进行Web程序权限的过滤   
*
***********************************************************************/



import java.io.IOException;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.easycoding.framework.context.WebContext;
import com.easycoding.framework.web.WebContextHelper;
import com.framework.web.context.WebApplicationContext;
public class NoFilterChecker implements Filter{

	@SuppressWarnings("unused")
	private String defaultServerURI = "";
	
	private String gonglueServerURI = "";
	
	private String zixunServerURI = "";
	
	//Filter config
	private FilterConfig config;
	
	public void init(FilterConfig config) throws ServletException {
		this.config = config;
		defaultServerURI = config.getInitParameter("defaultServerURI");
		gonglueServerURI = config.getInitParameter("gonglueServerURI");
		zixunServerURI = config.getInitParameter("zixunServerURI");
	}
	
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		ServletContext servletContext = this.config.getServletContext();
		HttpServletRequest httpServletRequest = (HttpServletRequest) request;
		HttpServletResponse httpServletResponse = (HttpServletResponse) response;
		//WebApplicationContext初始化
		WebApplicationContext.initialize(servletContext,httpServletRequest, httpServletResponse);
		WebContext.init();
		Map<String, Object> servletkeeper = WebContext.currentContext().getKeeper();
		servletkeeper.put(WebContextHelper.REQUEST, request);
		servletkeeper.put(WebContextHelper.RESPONSE, response);
		servletkeeper.put(WebContextHelper.SERVLET_CONTEXT, servletContext);
		servletkeeper.put(WebContextHelper.SESSION, ((HttpServletRequest)request).getSession());
		Object city = httpServletRequest.getSession().getAttribute("city");
		if(city == null)
			httpServletRequest.getSession().setAttribute("city", "厦门");
		 // 获取域名  
        String serverName = request.getServerName();  
        // 获取请求路径  
        String path = httpServletRequest.getRequestURI();  
        if (serverName.equals(gonglueServerURI)){//判断是否攻略
        	 httpServletRequest.getRequestDispatcher("/gonglue"+path).forward(request,response);  
                 return;  
        }
        else if(serverName.equals(zixunServerURI)){//判断是否资讯
        	 httpServletRequest.getRequestDispatcher("/zixun"+path).forward(request,response);  
                 return;  
        }
        chain.doFilter(request, response);
		return;
	}
	public void destroy() {

	}
	
}
